/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skv.db.daten.entities;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import skv.db.abst.SkvEntity;
import skv.db.iface.DbHandler;

/**
 * Stellt die Fächer Tabelle dar.
 * @author Alex
 */
public class Fach extends SkvEntity{

    private String fachname;

    public Fach(String fachname, DbHandler dbh) {
        
        this.fachname = fachname;
        this.dbh = dbh;
        this.create();
    }
    public Fach(int id, DbHandler dbh){
        
        this.dbh = dbh;
        this.id = id;
        getMyData();
    }

    @Override
    public String toString() {
        return getFachname();
    }
    
    @Override
    public void create() {
        
        dbh.doQuery("INSERT INTO `skv`.`faecher` (`fachid`, `fachname`) "
                + "VALUES (NULL, '"+this.getFachname()+"');");
        getLastInsertID();
    }

    @Override
    public void update() {
        dbh.doQuery("UPDATE `skv`.`faecher` SET"
                + " `fachname` = '"+this.getFachname()+"' "
                + "WHERE `faecher`.`fachid` = "+this.getId()+";");
    }
 
    @Override
    public void getMyData() {
        ResultSet rs = dbh.doQuery("SELECT * FROM faecher WHERE fachid = "+this.getId());
        try {
            rs.next();
            this.fachname = rs.getString("fachname");
        } catch (SQLException ex) {
            Logger.getLogger(Fach.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getFachname() {
        return fachname;
    }

    public void setFachname(String fachname) {
        this.fachname = fachname;
    }

    @Override
    public boolean isUnique() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete() {
        ResultSet rs = dbh.doQuery("DELETE FROM `skv`.`faecher` WHERE `skv`.`faecher`.`fachid`="+this.id);
        
    }
    
}
